from pyecharts.charts import Line
from pyecharts import options as opts
import json

print("折线图")
with open("美国.txt", "r", encoding="utf-8") as f:
    data = f.read()
with open("日本.txt", "r", encoding="utf-8") as f:
    data2 = f.read()
with open("印度.txt", "r", encoding="utf-8") as f:
    data3 = f.read()
print(data)
data = data.replace("jsonp_1629344292311_69436(", "")
data2 = data2.replace("jsonp_1629350871167_29498(", "")
data3 = data3.replace("jsonp_1629350745930_63180(", "")
print(data)
data = data[:-2]
data2 = data2[:-2]
data3 = data3[:-2]
print(data)
data = json.loads(data)
data2 = json.loads(data2)
data3 = json.loads(data3)
print(type(data), data)
data = data["data"][0]["trend"]
data2 = data2["data"][0]["trend"]
data3 = data3["data"][0]["trend"]
print(type(data), data)
x_data = data["updateDate"]
print(type(x_data), x_data)
y_data = data["list"][0]["data"]
y_data2 = data2["list"][0]["data"]
y_data3 = data3["list"][0]["data"]
print(type(y_data), y_data)
x_data = data["updateDate"][:314]
print(type(x_data), x_data)
y_data = data["list"][0]["data"][:314]
y_data2 = data2["list"][0]["data"][:314]
y_data3 = data3["list"][0]["data"][:314]
print(type(y_data), y_data)

line = (Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
    series_name="美国确诊人数",
    y_axis=y_data,
    symbol_size=10,
    label_opts=opts.LabelOpts(is_show=False),
    linestyle_opts=opts.LineStyleOpts(width=2)
)
.add_yaxis(
    series_name="日本确诊人数",
    y_axis=y_data2,
    symbol_size=10,
    label_opts=opts.LabelOpts(is_show=False),
    linestyle_opts=opts.LineStyleOpts(width=2)
)
.add_yaxis(
    series_name="印度确诊人数",
    y_axis=y_data3,
    symbol_size=10,
    label_opts=opts.LabelOpts(is_show=False),
    linestyle_opts=opts.LineStyleOpts(width=2)
)
.set_global_opts(
    title_opts=opts.TitleOpts(title="2020年 印🇮🇳美🇺🇸日🇯🇵 累计确诊人数对比图", pos_left="center"),
    xaxis_opts=opts.AxisOpts(name="时间"),
    yaxis_opts=opts.AxisOpts(name="累计确诊人数"),
    legend_opts=opts.LegendOpts(pos_left="70%")
)
)
line.render(path="render2.html")
